package com.xjh.uitl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDao {

    JDBCUtil jdbcUtil = new JDBCUtil();
    Connection c;

    public LoginDao() throws SQLException, ClassNotFoundException {
        c = jdbcUtil.getConn();
    }

    public boolean loginCheck(String username, String password) throws SQLException {
        String sql = "select * from user where username=? and password=?";
        PreparedStatement ps = c.prepareStatement(sql);
        ps.setString(1, username);
        ps.setString(2, password);
        ResultSet resultSet = ps.executeQuery();

        return resultSet.next();

    }



    public int insert() throws Exception {
        JDBCUtil jdbcUtil1=new JDBCUtil();
        Connection conn = jdbcUtil1.getConn();
        String sql="insert into user values ('zhangsan','123')";
        PreparedStatement ps = conn.prepareStatement(sql);
        int i = ps.executeUpdate();
        return i;
    }


    public void createTable()throws Exception{
        JDBCUtil jdbcUtil1=new JDBCUtil();
        Connection conn = jdbcUtil1.getConn();
        String sql="create table user(username varchar(255),password varchar(255))";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.executeUpdate();
    }

}
